﻿//===============================================================================
// Microsoft patterns & practices
// Windows Phone 7 Developer Guide
//===============================================================================
// Copyright © Microsoft Corporation.  All rights reserved.
// This code released under the terms of the 
// Microsoft patterns & practices license (http://wp7guide.codeplex.com/license)
//===============================================================================


using ConfApp.Services.ConfDataService;
using ConfApp.Services.Mock;
using ConfApp.Services.Podcasts;
using ConfApp.Services.Twitter;


namespace ConfApp.Services
{
    using System;
    using System.Windows;
    using Funq;
    //using RegistrationService;
    //using Stores;
    //using SurveysService;
    using ViewModels;

    public class ContainerLocator : IDisposable
    {
        private bool disposed;

        public ContainerLocator()
        {
            this.Container = new Container();
            this.ConfigureContainer();
        }

        public Container Container { get; private set; }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            if (disposing)
            {
                this.Container.Dispose();
            }

            this.disposed = true;
        }

        private void ConfigureContainer()
        {
            //this.Container.Register("ServiceUri",
            //            "http://127.0.0.1:81/DataService.svc/Data");
            //this.Container.Register("RegistrationServiceUri",
            //    new Uri("http://127.0.0.1:81/DataService.svc/"));

            this.Container.Register("ServiceUri",
                        "http://sin11capp.cloudapp.net/DataService.svc/Data");
            this.Container.Register("RegistrationServiceUri",
                new Uri("http://sin11capp.cloudapp.net/DataService.svc/"));
            this.Container.Register<ISettingsStore>(c => new SettingsStore());

            this.Container.Register<ITwitterServiceClient>(
                c => new TwitterServiceClient());

            this.Container.Register<ITwitterStore>(
                c => new TwitterStore());

            this.Container.Register<ITwitterUpdateService>(
                c => new TwitterUpdateService(
                    c.Resolve<ITwitterServiceClient>(),
                    c.Resolve<ITwitterStore>()));

            this.Container.Register<IPodcastServiceClient>(
                c => new PodcastServiceClient());

            this.Container.Register<IPodcastStore>(
                c => new PodcastStore());

            this.Container.Register<IPodcastUpdateService>(
                c => new PodcastUpdateService(
                    c.Resolve<IPodcastServiceClient>(),
                    c.Resolve<IPodcastStore>()));

            this.Container.Register<INavigationService>(_ =>
                new ApplicationFrameNavigationService(((App)Application.Current).RootFrame));

            // LocationService:
            // 1. Registration
            //this.Container.Register<ILocationService>(c => new LocationService(c.Resolve<ISettingsStore>()));

            // 2. Starts the service by trying to get the current location
            //this.Container.Resolve<ILocationService>().TryToGetCurrentLocation();

            // View Models
            this.Container.Register(
                c => new AppSettingsViewModel(
                         c.Resolve<ISettingsStore>(),
                         c.Resolve<IRegistrationServiceClient>(),
                         c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);

            this.Container.Register<IConfDataStoreLocator>(
                c => new ConfDataStoreLocator());

            this.Container.Register<IConfDataServiceClient>(
                c => new ConfDataServiceClient(
                    c.ResolveNamed<string>("ServiceUri"))
                    );

            this.Container.Register<IConfDataUpdateService>(
                c=> new ConfDataUpdateService(
                    c.Resolve<IConfDataServiceClient>(),
                    c.Resolve<IConfDataStoreLocator>(),
                    c.Resolve<ISettingsStore>())
                );
            
            this.Container.Register(
                c => new MainViewModel(
                            c.Resolve<IConfDataStoreLocator>(),
                            c.Resolve<IConfDataUpdateService>(),
                            c.Resolve<ITwitterStore>(),
                            c.Resolve<ITwitterUpdateService>(),
                            c.Resolve<INavigationService>()))
                    .ReusedWithin(ReuseScope.None);

            this.Container.Register(
                c => new SessionListViewModel(
                            c.Resolve<IConfDataStoreLocator>(),
                            c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);

            this.Container.Register(
                c => new SessionDetailsViewModel(
                            c.Resolve<IConfDataStoreLocator>(),
                            c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);

            this.Container.Register(
                c => new SpeakerListViewModel(
                            c.Resolve<IConfDataStoreLocator>(),
                            c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);


            this.Container.Register(
                c => new SpeakerDetailsViewModel(
                            c.Resolve<IConfDataStoreLocator>(),
                            c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);

            this.Container.Register(
                c => new TrackListViewModel(
                    c.Resolve<IConfDataStoreLocator>(),
                    c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);

            this.Container.Register(
                c => new PodcastListViewModel(
                    c.Resolve<IPodcastStore>(),
                    c.Resolve<IPodcastUpdateService>(),
                    c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);


            this.Container.Register(
                c => new PodcastViewModel(
                    c.Resolve<IPodcastStore>(),
                    c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);

            this.Container.Register(
                c => new Map0ViewModel(
                    c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);

            this.Container.Register(
                c => new Map1ViewModel(
                    c.Resolve<INavigationService>()))
                .ReusedWithin(ReuseScope.None);
            //this.Container.Register(
            //    c => new FilterSettingsViewModel(
            //             c.Resolve<IRegistrationServiceClient>(),
            //             c.Resolve<INavigationService>(),
            //             c.Resolve<ISurveyStoreLocator>()))
            //    .ReusedWithin(ReuseScope.None);

            //this.Container.Register<ISurveysSynchronizationService>(
            //    c => new SurveysSynchronizationService(
            //             c.Resolve<ISurveysServiceClient>,
            //             c.Resolve<ISurveyStoreLocator>()));
            //// The ONLY_PHONE symbol is only defined in the "OnlyPhone" configuration to run the phone project standalone
            //this.Container.Register<ISurveysServiceClient>(c => new SurveysServiceClientMock(c.Resolve<ISettingsStore>()));
            //this.Container.Register<IRegistrationServiceClient>(
            //    c => new RegistrationServiceClientMock(c.Resolve<ISettingsStore>()));
            this.Container.Register<IRegistrationServiceClient>(
                c => new RegistrationServiceClient(c.ResolveNamed<Uri>("RegistrationServiceUri"), c.Resolve<ISettingsStore>()));
            //this.Container.Register<ISurveysServiceClient>(c => new SurveysServiceClient(c.ResolveNamed<Uri>("ServiceUri"), c.Resolve<ISettingsStore>()));
            //this.Container.Register<IRegistrationServiceClient>(c => new RegistrationServiceClient(c.ResolveNamed<Uri>("RegistrationServiceUri"), c.Resolve<ISettingsStore>()));

        }
    }
}
